﻿@using Nop.Core
@using Nop.Services.Common
@{
    const string panelId = "report-equipment-box";
    const string hidePanelAttributeName = "HideBestsellersBriefReportByQuantityPanel";
    var hidePanel = EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.GetAttribute<bool>(hidePanelAttributeName);
    var gridPageSize = 5;
}

<div class="box box-info @if(hidePanel){<text>collapsed-box</text>}" id="@panelId">
    <div class="box-header with-border">
        <h3 class="box-title">
            <i class="fa fa-star"></i>
            维修设备统计
        </h3>
        <div class="box-tools pull-right">
            <button class="btn btn-xs btn-info btn-flat margin-r-5" onclick="javascript: window.location.href='@Url.Action("ReportByEquipmentList","Maintenance")';">更多...</button>
            <button class="btn btn-box-tool" data-widget="collapse">
                @if (hidePanel)
                {
                    <text><i class="fa fa-plus"></i></text>
                }
                else
                {
                    <text><i class="fa fa-minus"></i></text>
                }
            </button>
        </div>
    </div>
    <div class="box-body">
        <div id="bestsellers-byequipment-grid"></div>

        <script>
            $(document).ready(function () {
                $('#@panelId').on('click', 'button[data-widget="collapse"]', function () {
                    var collapsed = !$('#@panelId').hasClass('collapsed-box');
                    saveUserPreferences('@(Url.Action("SavePreference", "Preferences"))', '@hidePanelAttributeName', collapsed);
                });

                $("#bestsellers-byequipment-grid").kendoGrid({
                    dataSource: {
                        type: "json",
                        transport: {
                            read: {
                                url: "@Html.Raw(Url.Action("ReportByEquipments", "Maintenance"))",
                                type: "POST",
                                dataType: "json",
                                data: addAntiForgeryToken
                            }
                        },
                        schema: {
                            data: "Data",
                            total: "Total",
                            errors: "Errors"
                        },
                        requestEnd: function (e) {
                            if (e.type == "read") {
                                var response = e.response;
                                if (response) {
                                    //store extra data
                                    reportE = e.response["ExtraData"];
                                }
                            }
                        },
                        error: function(e) {
                            display_kendoui_grid_error(e);
                            // Cancel the changes
                            this.cancelChanges();
                        },
                        pageSize: @(gridPageSize),
                        serverPaging: true,
                        serverFiltering: true,
                        serverSorting: true
                    },
                    pageable: {
                        refresh: true,
                        @Html.Partial("_GridPagerMessages")
                    },
                    scrollable: false,
                    dataBound: onDataBoundE,
                    columns: [{
                        field: "Name",
                        title: "名称",
                        headerAttributes: { style: "text-align:center" },
                    },{
                        field: "Manufacturer",
                        title: "品牌",
                        headerAttributes: { style: "text-align:center" },
                    },{
                        field: "Specification",
                        title: "规格型号",
                        headerAttributes: { style: "text-align:center" },
                    },{
                        field: "Amount",
                        title: "次数",
                        headerAttributes: { style: "text-align:center" },
                        attributes: { style: "text-align:center" },
                        footerTemplate: '<div style="text-align:center;"><span id=\"e-total-amount\"></span></div>'
                    }, {
                        field: "HoursString",
                        title: "工时",
                        headerAttributes: { style: "text-align:center" },
                        attributes: { style: "text-align:right" },
                        footerTemplate: '<div style="text-align:right;"><span id=\"e-total-hour\"></span></div>'
                    }, {
                        field: "Per",
                        title: "效率",
                        headerAttributes: { style: "text-align:center" },
                        attributes: { style: "text-align:right" },
                    }]
                });
            });

            function onDataBoundE(e) {
                //update order totals summary
                if (reportE != "") {
                    $('#e-total-amount').text(reportE['Amount']);
                    $('#e-total-hour').text(reportE['HoursString']);
                }
            }
        </script>
    </div>
</div>